#include<iostream>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#define PAGE_LEN 4000
using namespace std;
int main(int argc,char *argv[2])
{
    if(argc!=2)
    {
        cout<<"Usage:"<<argv[0]<<"filename"<<endl;
        return 1;
    }
    int fd=open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0666);
    if(fd<0)
    {
        cout<<"errro failed..."<<argv[1]<<endl;
        return 1;
    }
    //文件映射
    char* mapfile=(char*)::mmap(nullptr,PAGE_LEN,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if(mapfile==MAP_FAILED)
    {
        cout<<"failed mmap file:"<<argv[1]<<endl;
        return 1;
    }
    //进行文件操作
    for(char c='a';c<'z';c++)
    {
        mapfile[c-'a']=c;
        sleep(3);
    }
    //关闭映射
    if(::munmap(mapfile,PAGE_LEN)==-1)
    {
        cout<<"failed to munmap file"<<argv[1]<<endl;
        return 3;
    }
}